<?php
// +----------------------------------------------------------------------
// | Author: yaoyihong <510974211@qq.com>
// +----------------------------------------------------------------------
namespace addon\mall\logic;
use app\common\logic\LogicBase;
use \tpfcore\Core;
use think\Db;

class CashOutLog extends LogicBase
{
	public function getCashOutLog($data=[]){
		$result = Core::loadModel($this->name)->getList($data);
		return $result;
	}
	public function delCashOutLog($data=[]){
		$result = self::deleteObject($data,true);
		if($result){
			return [0,"success"];
		}
		return [-1,"操作失败"];
	}
	public function handle($data){
		$type=empty($data['type'])?"":$data['type'];
		//拒绝提现
		if($type == "refuse"){
			Db::startTrans();
			try{
				$result = self::updateObject(["id"=>$data['id']],["reason"=>$data['reason'],"status"=>4]);
				if($result){		//提现失败，把钱还回去
					$listCashOut = self::getOneObject(["id"=>$data["id"]]);
					if($listCashOut){
						$money = $listCashOut['money'];
						$result = Core::loadModel("User","backend","logic")->where(["id"=>$listCashOut['user_id']])->setInc("profit",$money);
					}
				}
				if(!$result){
					throw new \Exception("操作失败", 1);
				}
				Db::commit();
			}catch(\Exception $e){
				Db::rollback();
				return [-1,$e->getMessage()];
			}
			
		}
		//成功提现
		if($type== "sure"){
			$result = self::updateObject(["id"=>$data['id']],["status"=>1]);	
		}
		if($result){
			return [0,"操作成功"];
		}
		return [-1,"操作失败"];
	}
}